package com.itms.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itms.entity.CustomerProject;
import com.itms.entity.ErrorMsg;
import com.itms.enums.TableName;
import com.itms.mapper.CustomerMapper;
import com.itms.mapper.CustomerProjectMapper;
import com.itms.mapper.ProjectMapper;
import com.itms.mapper.TableDataSyncMapper;
import com.itms.pojo.Project;
import com.itms.pojo.TableDataSync;
import com.itms.service.BasicDataService;
import com.itms.service.LogService;
import com.itms.service.SycService;
import com.itms.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@EnableScheduling
public class ProjectJob {

    @Reference
    private BasicDataService basicDataService;
    @Autowired
    private TableDataSyncMapper tableDataSyncMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private LogService logService;
    @Autowired
    private SycService sycService;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerProjectMapper customerProjectMapper;

    @Scheduled(initialDelayString = "${ProjectJob.initialDelayString}",fixedDelayString="${ProjectJob.fixedDelayString}" )
    public void deal(){
        try {
            List<ErrorMsg> errorList = new ArrayList<>();

            Date mdb = basicDataService.getTableSyncByTableName(TableName.MDBProject.toString());

            Date itms = this.sycService.getMaxSyncDate(TableName.MDBProject.toString());

            if(mdb==null){
                return ;
            }

            if (itms!=null && TimeUtils.getDateSequence(mdb).equals( TimeUtils.getDateSequence(itms))) {
                return;
            }

            List<Project> list = basicDataService.getProjectList(itms);
            //遍历mdb查询数据
            for (Project p : list) {
                if(!"SGM".equals(p.getCustomerCode())){
                    continue;
                }
                boolean flag = false;
                //itms对应的project
                com.itms.entity.Project project = projectMapper.getProjectByProjectCode(p.getProjectCode(),null);
                String customerCode = p.getCustomerCode();
                com.itms.entity.Customer customer = null;
                if (customerCode!=null){
                    flag =true ;
                    customer = this.customerMapper.selectByCustomerCode(customerCode,"1");
                }
                //不存在则对itms执行添加
                if (project == null) {
                    project = new com.itms.entity.Project();
                    project.setProjectCode(p.getProjectCode());
                    project.setProjectName(p.getProjectName());
                    project.setCreateDate(new Date());
                    project.setIsValid(p.getIsValid());
                    project.setLastUpdateDate(new Date());
                    project.setCreateUserId(new BigDecimal(9999));
                    projectMapper.insertSelective(project);

                } else {     //存在则执行更新
                    project.setProjectName(p.getProjectName());
                    project.setProjectCode(p.getProjectCode());
                    project.setIsValid(p.getIsValid());
                    project.setLastUpdateDate(new Date());
                    project.setLastUpdateUserId(new BigDecimal(9999));
                    projectMapper.updateProjectById(project);

                }
                if ((!flag )|| customer == null )continue;
                CustomerProject cp = this.customerProjectMapper.findByProjectCustomerId(project.getId(),customer.getId());
                if (cp == null) {
                    cp = new CustomerProject();
                    cp.setCustomerId(customer.getId());
                    cp.setProjectId(project.getId());
                    cp.setCreateDate(new Date());
                    cp.setCreateUserId(new BigDecimal(9999));
                    this.customerProjectMapper.insertSelective(cp);
                }
            }
            if (errorList.size() == 0) {
                this.sycService.save(mdb, TableName.MDBProject.toString());
            }
            this.logService.saveErrorList(errorList,TableName.MDBProject.toString(),this.getClass().getName());



        }catch(Exception e){
            e.printStackTrace();
            this.logService.saveLog(e,TableName.MDBProject.toString(),this.getClass().getName());
        }
    }
}
